package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.VenueDrawingAuditMapper;
import com.eastfair.venueservice.dto.VenueDrawingAuditDTO;
import com.eastfair.venueservice.entity.VenueDrawingAudit;
import com.eastfair.venueservice.service.VenueDrawingAuditService;
import com.eastfair.venueservice.vo.VenueDrawingAuditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸审核
 * </p>
 *
 * @author dq
 * @date 2022-10-31
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class VenueDrawingAuditServiceImpl extends SuperServiceImpl<VenueDrawingAuditMapper, VenueDrawingAudit> implements VenueDrawingAuditService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<VenueDrawingAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(VenueDrawingAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<VenueDrawingAuditVO> listVenueDrawingAuditVO(VenueDrawingAuditDTO query) {
        log.info("listVenueDrawingAuditVO - 查询图纸审核列表VO, query={}", query);
        List<VenueDrawingAudit> list = listVenueDrawingAudit(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(venueDrawingAudit -> BeanUtil.toBean(venueDrawingAudit, VenueDrawingAuditVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<VenueDrawingAudit> listVenueDrawingAudit(VenueDrawingAuditDTO query) {
        QueryWrapper<VenueDrawingAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(VenueDrawingAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getExhibitionManageId() != null, VenueDrawingAudit::getExhibitionManageId, query.getExhibitionManageId())
                .eq(query.getVenueReportId() != null, VenueDrawingAudit::getVenueReportId, query.getVenueReportId())
                .eq(StrUtil.isNotBlank(query.getSpaceCode()), VenueDrawingAudit::getSpaceCode, query.getSpaceCode())
                .eq(query.getDrawingId() != null, VenueDrawingAudit::getDrawingId, query.getDrawingId())
        ;
        return list(queryWrapper);
    }

    @Override
    public VenueDrawingAudit saveVenueDrawingAudit(VenueDrawingAuditDTO param) {
        log.info("saveVenueDrawingAudit - 保存图纸审核, param={}", param);
        VenueDrawingAudit venueDrawingAudit = BeanUtil.toBean(param, VenueDrawingAudit.class);
        save(venueDrawingAudit);
        return venueDrawingAudit;
    }

    @Override
    public boolean saveBatch(List<VenueDrawingAuditDTO> list) {
        log.info("saveBatch - 批量保存图纸审核, list={}", list);
        list.forEach(this::saveVenueDrawingAudit);
        return true;
    }

    @Override
    public boolean submitBatch(List<VenueDrawingAuditDTO> list) {
        log.info("submitBatch - 批量提交图纸审核, list={}", list);
        if (list == null || list.isEmpty()) {
            return true;
        }
        // 移除报馆中此展厅，此图纸类型的图纸审核记录
        VenueDrawingAuditDTO param = list.get(0);
        removeByVenueReportId(param.getVenueReportId(), param.getSpaceCode(), param.getDrawingId());
        // 批量保存图纸审核
        return saveBatch(list);
    }

    @Override
    public boolean removeByVenueReportId(Long venueReportId, String spaceCode, Long drawingId) {
        log.info("removeByVenueReportId - 报馆ID移除展厅中图纸分类ID指定的图纸审核记录, venueReportId={}, spaceCode={}, drawingId={}",
                venueReportId, spaceCode, drawingId);
        VenueDrawingAuditDTO param = new VenueDrawingAuditDTO();
        param.setVenueReportId(venueReportId);
        param.setSpaceCode(spaceCode);
        param.setDrawingId(drawingId);
        List<VenueDrawingAudit> list = listVenueDrawingAudit(param);
        if (list == null || list.isEmpty()) {
            return true;
        }
        return removeByIdsOfLogic(list);
    }
}
